package com.blb.thread;

/**
 * 等待和通知案例
 */
public class WaitNotifyDemo {

    public synchronized static void testWait(){
        try {
            System.out.println(Thread.currentThread().getName() +"开始等待");
            //让线程等待
            WaitNotifyDemo.class.wait();
            System.out.println(Thread.currentThread().getName() +"执行完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        WaitNotifyDemo demo = new WaitNotifyDemo();
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                WaitNotifyDemo.testWait();
            }).start();
        }
        //过5s主线程通知子线程执行
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //通知需要上上锁的代码内执行
        synchronized (WaitNotifyDemo.class) {
            WaitNotifyDemo.class.notifyAll();
        }
    }
}
